package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.controller
 * @date 2019/6/26 15:03
 * @description:
 **/
@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 查询规格组
     * @param cid
     * @return
     */
    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCid(@RequestParam("id")Long cid){
        return ResponseEntity.ok(specService.querySpecGroupByCid(cid));
    }

    /**
     * 查询规格参数
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamByGid(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching
            ){
        return ResponseEntity.ok(specService.querySpecParamByGid(gid,cid,searching));
    }

    /**
     * 新增规格组
     * @param dto
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO dto){
        specService.saveSpecGroup(dto);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改规格组
     * @param dto
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO dto){
        specService.updateSpecGroup(dto);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据spuID查询规格组和规格参数
     * @param cid
     * @return sku的集合
     */
    @GetMapping("list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecs(@RequestParam("id") Long cid){
        return ResponseEntity.ok(specService.querySpecs(cid));
    }
}
